﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using DAL = AccessDAL;
using CommonFunc;
using DBUtility;


namespace BLL
{
    public class Product
    {
        DAL.ProductTableAdapters.ProductTableAdapter DP =
            new DAL.ProductTableAdapters.ProductTableAdapter();


        MODEL.Product _MP = new MODEL.Product();
        
        MODEL.ProductClass MPC = new MODEL.ProductClass();
        MODEL.ProductClass _MPC = new MODEL.ProductClass();

        BLL.ProductClass BPC = new ProductClass();


        #region GetDataByID
        /// <summary>
        /// GetDataByID
        /// </summary>
        /// <param name="MP"></param>
        /// <returns></returns>
        public MODEL.Product GetDataByID(MODEL.Product MP)
        {
            try
            {
                DataTable DT = DP.GetDataByID(MP.Product_ID);

                _MP.Product_ClassCode = DT.Rows[0]["Product_ClassCode"].ToString();
                _MP.Product_ClassID = Convert.ToInt32(DT.Rows[0]["Product_ClassID"]);
                _MP.Product_Content = DT.Rows[0]["Product_Content"].ToString();
                _MP.Product_DateTime = Convert.ToDateTime(DT.Rows[0]["Product_DateTime"]);
                _MP.Product_ID = Convert.ToInt32(DT.Rows[0]["Product_ID"]);
                _MP.Product_Index = Convert.ToBoolean(DT.Rows[0]["Product_Index"]);
                _MP.Product_Intro = DT.Rows[0]["Product_Intro"].ToString();
                _MP.Product_IsDel = Convert.ToBoolean(DT.Rows[0]["Product_IsDel"]);
                _MP.Product_IsLock = Convert.ToBoolean(DT.Rows[0]["Product_IsLock"]);
                _MP.Product_MasterID = Convert.ToInt32(DT.Rows[0]["Product_MasterID"]);
                _MP.Product_Order = Convert.ToInt32(DT.Rows[0]["Product_Order"]);
                _MP.Product_SmallThumb = DT.Rows[0]["Product_SmallThumb"].ToString();
                _MP.Product_Thumbnail = DT.Rows[0]["Product_Thumbnail"].ToString();
                _MP.Product_Title = DT.Rows[0]["Product_Title"].ToString();

                return _MP;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 添加产品
        /// <summary>
        /// 添加产品
        /// </summary>
        /// <param name="MP"></param>
        public void Add(MODEL.Product MP)
        {
            try
            {
                string ClassCode;
                int OrderNum;

                DataTable DT = DP.GetDataByTitle_ClassID(MP.Product_Title, MP.Product_ClassID);
                if (DT.Rows.Count != 0)
                {
                    throw new Exception("同一类别下，不能有重复标题");
                }

                MPC.PC_ID = MP.Product_ClassID;
                _MPC = BPC.GetDataByID(MPC);
                ClassCode = _MPC.PC_ClassCode;

                object TempOrder = DP.GetMaxOrder();
                if (TempOrder == null)
                {
                    OrderNum = 1;
                }
                else
                {
                    OrderNum = Convert.ToInt32(TempOrder) + 1;
                }

                DP.Add(MP.Product_Title, MP.Product_ClassID, ClassCode, MP.Product_Thumbnail,
                    MP.Product_Intro, MP.Product_Content, MP.Product_MasterID, MP.Product_DateTime,
                    MP.Product_IsLock, MP.Product_IsDel, OrderNum, MP.Product_SmallThumb, MP.Product_Index);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 修改产品
        /// <summary>
        /// 修改产品
        /// </summary>
        /// <param name="MP"></param>
        public void Mod(MODEL.Product MP)
        {
            try
            {
                string ClassCode;

                MPC.PC_ID = MP.Product_ClassID;
                _MPC = BPC.GetDataByID(MPC);
                ClassCode = _MPC.PC_ClassCode;

                DP.Mod(MP.Product_Title, MP.Product_ClassID, ClassCode, MP.Product_Intro, MP.Product_Content,
                    MP.Product_IsLock, MP.Product_Index, MP.Product_ID);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 删除产品
        /// <summary>
        /// 删除产品
        /// </summary>
        /// <param name="MP"></param>
        public void Del(MODEL.Product MP)
        {
            try
            {
                _MP = GetDataByID(MP);
                string Thumb = _MP.Product_Thumbnail;
                string SmallThumb = _MP.Product_SmallThumb;

                DP.Del(MP.Product_ID);

                if (Thumb != null)
                {
                    CommonFunc.CommonFunction.DelFile(
                        HttpContext.Current.Server.MapPath("~/") + "UploadImage/" + Thumb);
                }
                if (SmallThumb != null)
                {
                    CommonFunc.CommonFunction.DelFile(
                        HttpContext.Current.Server.MapPath("~/") + "UploadImage/" + SmallThumb);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 修改产品图片
        public void ModImage(MODEL.Product MP)
        {
            try
            {
                _MP = GetDataByID(MP);
                string Thumb = _MP.Product_Thumbnail;
                string SmallThumb = _MP.Product_SmallThumb;

                DP.ModThumb(MP.Product_Thumbnail, MP.Product_SmallThumb, MP.Product_ID);

                if (Thumb != null)
                {
                    CommonFunc.CommonFunction.DelFile(
                        HttpContext.Current.Server.MapPath("~/") + "UploadImage/" + Thumb);
                }
                if (SmallThumb != null)
                {
                    CommonFunc.CommonFunction.DelFile(
                        HttpContext.Current.Server.MapPath("~/") + "UploadImage/" + SmallThumb);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 标记已锁定
        /// <summary>
        /// 标记已锁定
        /// </summary>
        /// <param name="MP"></param>
        public void MarkIsLock(MODEL.Product MP)
        {
            try
            {
                string SQLstr_IsLock = "UPDATE [Product] SET [Product_IsLock]= NOT [Product_IsLock] WHERE [Product_ID]=" + MP.Product_ID;

                AccHelper.ExecuteSQL(SQLstr_IsLock);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 标记已删除
        /// <summary>
        /// 标记已删除
        /// </summary>
        /// <param name="MP"></param>
        public void MarkIsDel(MODEL.Product MP)
        {
            try
            {
                string SQLstr_IsDel = "UPDATE [Product] SET [Product_IsDel]= NOT [Product_IsDel] WHERE [Product_ID]=" + MP.Product_ID;

                AccHelper.ExecuteSQL(SQLstr_IsDel);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 标记是否首页推荐
        /// <summary>
        /// 标记是否首页推荐
        /// </summary>
        /// <param name="MP"></param>
        public void MarkIsIndex(MODEL.Product MP)
        {
            try
            {
                string SQLstr_MarkIndex = "UPDATE [Product] SET [Product_Index] = NOT [Product_Index] WHERE [Product_ID]=" + MP.Product_ID;

                AccHelper.ExecuteSQL(SQLstr_MarkIndex);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion
    }
}
